package com.chinarelife.util;

import java.util.Arrays;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;

public class SM3Util
  extends GMBaseUtil
{
  public static byte[] hash(byte[] srcData)
  {
    SM3Digest digest = new SM3Digest();
    digest.update(srcData, 0, srcData.length);
    byte[] hash = new byte[digest.getDigestSize()];
    digest.doFinal(hash, 0);
    return hash;
  }
  
  public static boolean verify(byte[] srcData, byte[] sm3Hash)
  {
    byte[] newHash = hash(srcData);
    if (Arrays.equals(newHash, sm3Hash)) {
      return true;
    }
    return false;
  }
  
  public static byte[] hmac(byte[] key, byte[] srcData)
  {
    KeyParameter keyParameter = new KeyParameter(key);
    SM3Digest digest = new SM3Digest();
    HMac mac = new HMac(digest);
    mac.init(keyParameter);
    mac.update(srcData, 0, srcData.length);
    byte[] result = new byte[mac.getMacSize()];
    mac.doFinal(result, 0);
    return result;
  }
}
